package com.lwt.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.lwt.model.House;
import com.lwt.model.Users;
import com.lwt.model.Users.GroupL;
import com.lwt.model.Users.GroupR;
import com.lwt.service.UsersService;
import com.lwt.service.impl.UsersServiceImpl;
import com.lwt.util.Page;
import com.lwt.util.Result;

@RestController
@RequestMapping("/users")
public class UsersController {
	@Autowired
	UsersService userservice = new UsersServiceImpl();
	
	@RequestMapping(value="/login.do")
	public ModelAndView login(HttpSession session,@Validated(GroupL.class) Users user) {
		Users u = userservice.login(user);
		System.out.println("登录用户"+u);
		if (u != null) {
			session.setAttribute("loginuser", u);
			return new ModelAndView("houselist");
		} else {
			//登录失败
			return new ModelAndView("login");
		}
	}

	/**
	 * 注册用户
	 * @return
	 */
	@RequestMapping(value="/register.do")
	public ModelAndView register(@Validated(GroupR.class)Users user) {
		user.setIsadmin("不是");
		// 判断用户名是否存在
		if (userservice.check(user)==0) {			
			userservice.addUsers(user);
			return new ModelAndView("login");
		} else {
			//用户名已存在
			return new ModelAndView("login");
		}
	}
	/**
	 * 模糊查询用户
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/querylike.do")
	public Page queryLike(int pageSize, int pageNum, Users user){
		System.out.println("模糊查询"+user);
		Map<String, Object> usersinfo = new HashMap<String, Object>();
		usersinfo.put("pageSize", pageSize);
		usersinfo.put("pageNum", pageNum);
		usersinfo.put("name", user.getName());
		usersinfo.put("username", user.getUsername());
		Page page=userservice.queryLike(usersinfo);
		return page;
	}
	
	/**
	 * 准备更新用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/toupdate.do")
	public ModelAndView toupdate(Users user) {
		user= userservice.getUsers(user);
		Map<String,Object> rsMap = new HashMap<String,Object>();
		rsMap.put("data", user);
		return new ModelAndView("useredit",rsMap);
	}

	/**
	 * 更新租房信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/update.do")
	public Result update(Users user) {
		boolean a = userservice.update(user);
		return new Result();
	}
	/**
	 * 根据用户名和真实姓名更改密码
	 * 
	 * @return
	 */
	@RequestMapping(value = "/resetpwd.do")
	public ModelAndView resetpwd(String repassword,Users user){
		boolean a=false;
		if(repassword.equals(user.getPassword())){	
			System.out.println(user);
			a= userservice.resetPwd(user);
		}
		System.out.println(a);
		return new ModelAndView("login");
	}

}
